package com.zf.zfed.audit.ui

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.widget.TextView
import com.blankj.utilcode.util.BarUtils
import com.zf.zfed.audit.tools.ActivityCollector
import com.zf.zfed.audit.R
class WaitDialog(message: String = "", canCancelable: Boolean = false, context: Context = ActivityCollector.rootActivity()) {
    constructor(arg1: String) : this(arg1, false) {}

    private var dialog: Dialog

    init {
        dialog = Dialog(context)
        dialog.setContentView(R.layout.layout_wait_dialog)
        dialog.window?.let {
            it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        }
        dialog.setOnDismissListener {
            BarUtils.setNavBarVisibility(context as Activity, false)
        }
        dialog.setCancelable(canCancelable)
        val messageTextView: TextView = dialog.findViewById(R.id.tv_wait)
        messageTextView.text = message
    }

    fun show() {
        dialog.show()
    }

    fun hide() {
        dialog.dismiss()
    }

    fun isOpening(): Boolean {
        return dialog.isShowing
    }

    companion object {
        private var waitDialog: WaitDialog? = null

        fun show(context: Context? = null) {
            show("", false, context)
        }

        fun show(message: String, canCancelable: Boolean = false, context: Context? = null) {
            if (waitDialog == null) {
                waitDialog = if (context == null) {
                    WaitDialog(message, canCancelable)
                } else {
                    WaitDialog(message, canCancelable, context)
                }
                waitDialog?.show()
            }
        }

        fun hide() {
            waitDialog?.hide()
            waitDialog = null
        }

        fun isOpening(): Boolean {
            return waitDialog?.isOpening() ?: false
        }
    }
}